package com.example.netkey;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class MainActivity extends Activity {

    String  ipp="192.168.1.134";
    int     portt=8081;

    int textsize=10;


    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //
        EditText cfmdg=findViewById(R.id.ip);
        SharedPreferences sharedPreferencesp = getSharedPreferences("netkey", Context.MODE_PRIVATE);
        String netkey_ip = sharedPreferencesp.getString("ip", "");// 从SharedPreferences中获取保存的账号密码
        ipp=netkey_ip;
        cfmdg.setText(netkey_ip);


    }
    public  void ok(View view){
        EditText cmd=findViewById(R.id.cmd);
        String cmd_c=cmd.getText().toString();
        new Thread(new Runnable() {
            @Override
            public void run() {
                String host = ipp; // 服务器的IP地址
                int port = portt; // 服务器的端口号
                try (Socket socket = new Socket(host, port);
                     PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
                     BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                    // 发送字符串"000000"到服务器
                    writer.println("system"+cmd_c);
                    // 读取服务器响应的数据
                    String data = reader.readLine();
                    if (data != null) {
                        System.out.println("Received data from server: " + data);
                    }
                } catch (IOException e) {
                    System.err.println("Error in the client>>>>>>>>>>>>>>> : " + e.getMessage());
                    e.printStackTrace();
                }

            }
        }).start();
    }


    public  void add(View view){
        EditText cmd=findViewById(R.id.cmd);
        textsize=textsize+1;
        cmd.setTextSize(textsize);
    }

    public  void  remo(View view){
        EditText cmd=findViewById(R.id.cmd);
        textsize=textsize-1;
        cmd.setTextSize(textsize);
    }

    public  void  clear(View view){
        EditText cmd=findViewById(R.id.cmd);
        cmd.setText("");
    }

    public  void  exit(View view){
        finish();
    }

    public  void save_ip(View view){
        EditText cfmd=findViewById(R.id.ip);
        String ipc =cfmd.getText().toString();
        ipp=ipc;//


        SharedPreferences sharedPreferences = getSharedPreferences("netkey", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor_ip = sharedPreferences.edit();
        editor_ip.putString("ip", ipc);// 将账号密码保存到SharedPreferences中
        editor_ip.apply();


        Toast.makeText(MainActivity.this, "已保存IP:"+ipc, Toast.LENGTH_SHORT).show();


    }

    public  void  net(String commof){
        new Thread(new Runnable() {
            @Override
            public void run() {
                //
                String host = ipp; // 服务器的IP地址
                int port = portt; // 服务器的端口号

                try (Socket socket = new Socket(host, port);
                     PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
                     BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {

                    // 发送字符串"000000"到服务器
                    writer.println(commof);

                    // 读取服务器响应的数据
                    String data = reader.readLine();
                    if (data != null) {
                        System.out.println("Received data from server: " + data);
                    }

                } catch (IOException e) {
                    System.err.println("Error in the client>>>>>>>>>>>>>>> : " + e.getMessage());
                    e.printStackTrace();
                }

            }
        }).start();

    }


    public  void  task(View view){
        net("taskmgr");
    }
    public  void  reboot(View view){
        net("reboot");
    }
    public  void  shutdown(View view){
        net("shutdown");
    }
    public  void  shuangji(View view){
        net("shuangji");
    }
    public  void  danji(View view){
        net("danji");
    }
    public  void  mouse_l(View view){
        net(" mouse_l");
    }
    public  void   mouse_r(View view){
        net("mouse_r");
    }
    public  void  browser(View view){
        net("browser");
    }

    public  void  key(View view){
       // LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        //@SuppressLint("InflateParams") View popupView = inflater.inflate(R.layout.key, null);
        ///PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        Intent intent = new Intent(MainActivity.this, code.class);
        startActivity(intent);
    }

    public  void  up(View view){
        net("up");
    }

    public  void  left(View view){
        net("left");
    }

    public  void  right(View view){
        net("right");
    }

    public  void  down(View view){
        net("down");
    }

    public  void  unable(View view){
        net("unable");
    }







}
